//
//  HYCheyanchaoViewController.m
//  FamilyTravel
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCheyanchaoViewController.h"
#import "HYCHeadDocument.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HYCheyanchaoViewController ()
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *picArr;
@property (nonatomic, retain) NSString *nit;

@end

@implementation HYCheyanchaoViewController

- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [_picArr release];
    [_nit release];
    
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"使用说明", @"清除缓存", @"夜间模式", @"意见反馈", @"我的收藏", nil];
        self.picArr = [NSMutableArray arrayWithObjects:@"shuo.png", @"cun.png", @"day.png",@"yijian.png", @"shou.png", nil];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"yindao1"];
    
    [self.view addSubview:imageView];
    
    
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *strpath = [cachesPath stringByAppendingPathComponent:@"location.txt"];
    
    NSError *error = nil;
    self.nit = [NSString stringWithContentsOfFile:strpath encoding:NSUTF8StringEncoding error:&error];
    if (self.nit == nil) {
        self.nit = @"1";
    }
    
    
    
    self.tableView = ({
        UITableView *tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, (HEIGHT - 54 * 5) / 2.0f, WIDTH, 54 * 5) style:UITableViewStyleGrouped];
        tableV.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableV.delegate = self;
        tableV.dataSource = self;
        tableV.opaque = NO;
        tableV.backgroundColor = [UIColor clearColor];
        tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableV.bounces = NO;
        tableV.tag = 1000;
        tableV;
    });
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    self.navigationItem.leftBarButtonItem.title = @"返回";
    
}


// 区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
// height
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @" ";
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    LeftCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        
        cell = [[[LeftCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
        
    }
    if (indexPath.row == 2) {
        
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *strpath = [cachesPath stringByAppendingPathComponent:@"location.txt"];
        
        
        NSError *error = nil;
        [self.nit writeToFile:strpath atomically:YES encoding:NSUTF8StringEncoding error:&error];
        NSLog(@"%@", strpath);
        
        
        if ([self.nit isEqualToString:@"1"]) {
            cell.label.text = @"白天模式";
            cell.image.image = [UIImage imageNamed:@"night.png"];
            self.view.window.alpha = 1;
        } else {
            
            cell.image.image = [UIImage imageNamed:@"day.png"];
            cell.label.text = self.arr[indexPath.row];
            
            // 假夜间模式
            if (self.view.window.alpha == 1) {
                self.view.window.alpha = 0.3;
            }
        }
        
        
        
        
    } else if (indexPath.row == 1) {
        cell.label.text = self.arr[indexPath.row];
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        CGFloat size = [self folderSizeAtPath:sandBoxPath];
        cell.image.image = [UIImage imageNamed:@"cun.png"];
        cell.label.text = [NSString stringWithFormat:@"清理缓存(%.2fM)", size];
        cell.selectionStyle = NO;
    } else{
        cell.label.text = self.arr[indexPath.row];
        cell.image.image = [UIImage imageNamed:self.picArr[indexPath.row]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        HYCUseController *helpVC = [[HYCUseController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:helpVC];
        [helpVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
        [naVC release];
        [helpVC release];
    }
    if (indexPath.row == 1) {
        
        // 确定手机系统版本
        NSString *phoneVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"手机系统版本: %@", phoneVersion);
        NSLog(@"%ld", (long)phoneVersion.integerValue);
        
        if (phoneVersion.integerValue >= 8) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要清除缓存吗?" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
                NSString *sandBoxPath = [sandBox lastObject];
                [self clearCache:sandBoxPath];
                UITableView *tblView = (UITableView *)[self.view viewWithTag:1000];
                [tblView reloadData];
                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                
            }];
            
            [alert addAction:action1];
            [alert addAction:action2];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要清除缓存吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertV show];
            [alertV release];
            
        }
    }
    if (indexPath.row == 2){
        if ([self.nit isEqualToString:@"1"]) {
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:@"change" object:[UIColor blackColor] userInfo:nil];
            self.nit = @"2";
            [self.tableView reloadData];
        } else {
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:@"change" object:[UIColor clearColor] userInfo:nil];
            self.nit = @"1";
            [self.tableView reloadData];
        }
        
    }
    
    if (indexPath.row == 3) {
        // 确定手机系统版本
        NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"手机系统版本: %@", phoneVersion);
        
        NSLog(@"%ld", (long)phoneVersion.integerValue);
        
        if (phoneVersion.integerValue >= 8) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"如果本软件有任何问题请联系786782678@qq.com" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            
            [alert addAction:action1];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:nil message:@"如果本软件有任何问题请联系786782678@qq.com" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alertV show];
            [alertV release];
            
        }
        
    }
    //我的收藏
    if (indexPath.row == 4) {
        HYCCollectController *helpVC = [[HYCCollectController alloc] init];
        
        [self.navigationController pushViewController:helpVC animated:YES];
        
        NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"HYCLv.plist"];
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
        
        helpVC.arr = arr;
        
        
        NSString *secDocPath = [sandBoxPath stringByAppendingPathComponent:@"HYCCity.plist"];
        NSArray *SecArr = [NSKeyedUnarchiver unarchiveObjectWithFile:secDocPath];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (HYCSecondPushDisModel *tempModel in SecArr) {
            [tempArray addObject:tempModel];
        }
        
        helpVC.secArr = tempArray;
        if (helpVC.arr.count == 0 && helpVC.secArr.count == 0) {
            
            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"⚠️" message:@"你还没人任何收藏" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [aler show];
        }
        
        
        
        [helpVC release];
    }
    
}

// 清除缓存
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}

// 计算单个文件大小
- (CGFloat)fileSizeAtPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}

// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
    }
    
    return folderSize;
}

// 点击alert上的按钮执行的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        [self clearCache:sandBoxPath];
        UITableView *tblView = (UITableView *)[self.view viewWithTag:1000];
        [tblView reloadData];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
